# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
from pathlib import Path
from typing import Union

import paddle
from utils.hparams import hparams


class BaseExporter:
    def __init__(
        self, device: Union[str, (paddle.CPUPlace, paddle.CUDAPlace, str)] = None, cache_dir: Path = None, **kwargs
    ):
        self.device = (
            device
            if device is not None
            else str("cuda" if paddle.device.cuda.device_count() >= 1 else "cpu").replace("cuda", "gpu")
        )
        self.cache_dir: Path = (
            cache_dir.resolve() if cache_dir is not None else Path(__file__).parent.parent / "deployment" / "cache"
        )
        self.cache_dir.mkdir(parents=True, exist_ok=True)

    def build_spk_map(self) -> dict:
        if hparams["use_spk_id"]:
            with open(Path(hparams["work_dir"]) / "spk_map.json", "r", encoding="utf8") as f:
                spk_map = json.load(f)
            assert isinstance(spk_map, dict) and len(spk_map) > 0, "Invalid or empty speaker map!"
            assert len(spk_map) == len(set(spk_map.values())), "Duplicate speaker id in speaker map!"
            return spk_map
        else:
            return {}

    def build_model(self) -> paddle.nn.Layer:
        """
        Creates an instance of nn.Module and load its state dict on the target device.
        """
        raise NotImplementedError()

    def export_model(self, path: Path):
        """
        Exports the model to ONNX format.
        :param path: the target model path
        """
        raise NotImplementedError()

    def export_attachments(self, path: Path):
        """
        Exports related files and configs (e.g. the dictionary) to the target directory.
        :param path: the target directory
        """
        raise NotImplementedError()

    def export(self, path: Path):
        """
        Exports all the artifacts to the target directory.
        :param path: the target directory
        """
        raise NotImplementedError()
